package com.wl.java_spring_boot01.modules.account.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wl.java_spring_boot01.modules.account.dao.UserDao;
import com.wl.java_spring_boot01.modules.account.dao.UserRoleDao;
import com.wl.java_spring_boot01.modules.account.entity.Role;
import com.wl.java_spring_boot01.modules.account.entity.User;
import com.wl.java_spring_boot01.modules.account.service.UserService;
import com.wl.java_spring_boot01.modules.common.vo.Result;
import com.wl.java_spring_boot01.modules.common.vo.SearchVo;
import com.wl.java_spring_boot01.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author 汪林
 * @date 2020/8/20 14:11
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserRoleDao userRoleDao;

    //注册用户
    @Override
    @Transactional
    public Result<User> insertUser(User user) {
        User userTemp = userDao.getUserByUserName(user.getUserName());
        if (userTemp != null) {
            return new Result<User>(
                    Result.ResultStatus.FAILD.status, "User name is repeat.");
        }
        user.setCreateDate(LocalDateTime.now());
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        userDao.insertUser(user);

        userRoleDao.deleteUserRoleByUserId(user.getUserId());
        List<Role> roles = user.getRoles();
        if (roles != null && !roles.isEmpty()) {
            roles.stream().forEach(item -> {
                userRoleDao.insertUserRole(user.getUserId(), item.getRoleId());
            });
        }

        return new Result<User>(
                Result.ResultStatus.SUCCESS.status, "Insert success.", user);
    }

    //登录
    @Override
    public Result<User> login(User user) {
        //第一步：查询用户名
        User user_temp= userDao.getUserByUserName(user.getUserName());
        if (user_temp!=null&&
                user_temp.getPassword().equals(MD5Util.getMD5(user.getPassword()))){
            return new Result<User>(Result.ResultStatus.SUCCESS.status,
                    "login success",user);
        }
        return new Result<User>(Result.ResultStatus.FAILD.status,
                "userName or password fail",user);
    }

    //分页
    @Override
    public PageInfo<User> getUsersBySearchVo(SearchVo searchVo) {
        searchVo.initSearchVo();
        PageHelper.startPage(searchVo.getCurrentPage(),searchVo.getPageSize());
        return new PageInfo<User>(
                Optional.ofNullable(userDao.getUsersBySearchVo(searchVo))
                        .orElse(Collections.emptyList()));
    }

    //修改
    @Override
    @Transactional
    public Result<User> updateUser(User user) {
        //判断修改的用户名是否重复
        User user_temp= userDao.getUserByUserName(user.getUserName());
        if (user_temp!=null&&user_temp.getUserId()!=user.getUserId()){
            return new Result<User>(Result.ResultStatus.FAILD.status,
                    "userName repait.",user);
        }
        userDao.updateUser(user);
        userRoleDao.deleteUserRoleByUserId(user.getUserId());
        List<Role> roles = user.getRoles();
        if (roles != null && !roles.isEmpty()) {
            roles.stream().forEach(item -> {
                userRoleDao.insertUserRole(user.getUserId(), item.getRoleId());
            });
        }

        return new Result<User>(Result.ResultStatus.SUCCESS.status,
                "update success.",user);
    }

    //删除
    @Override
    @Transactional
    public Result<Object> deleteUser(int userId) {
        userDao.deleteUser(userId);
        userRoleDao.deleteUserRoleByUserId(userId);
        return new Result<Object>(Result.ResultStatus.SUCCESS.status,"delete success.");
    }

    //查询
    @Override
    public User getUserByUserId(int userId) {
       return userDao.getUserByUserId(userId);
    }
}
